package cn.heroes.weixin.commons;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.heroes.weixin.Contants;

/**
 * Http协议工具类
 * 
 * @author cq
 * 
 */
public class HttpUtils {

	private static final Logger logger = LoggerFactory
			.getLogger(HttpUtils.class);

	/*
	 * public static String get(String urlstr) { HttpURLConnection httpURLConn =
	 * null; InputStream in = null; try { URL url = new URL(urlstr); httpURLConn
	 * = (HttpURLConnection) url.openConnection();
	 * httpURLConn.setDoOutput(true); httpURLConn.setRequestMethod("GET");
	 * httpURLConn.setIfModifiedSince(999999999); //
	 * httpURLConn.setRequestProperty("Referer", "http://localhost:80");
	 * httpURLConn.setRequestProperty("User-Agent", "Dragon Ball");
	 * httpURLConn.connect(); in = httpURLConn.getInputStream(); return
	 * IOUtils.toString(in); } catch (Exception e) {
	 * logger.error("Cannot do http GET with url [{}]", urlstr); return ""; }
	 * finally { if (in != null) { try { in.close(); } catch (IOException e) {
	 * // do nothing } } if (httpURLConn != null) { httpURLConn.disconnect(); }
	 * } }
	 */

	/**
	 * 发起GET请求，并以字符串形式返回结果
	 * 
	 * @param url
	 *            请求的目的url
	 * @return 字符串形式的返回结果, 如果出错或没有结果则返回null.
	 */
	public static String get(String url) {
		HttpGet get = new HttpGet(url);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		InputStream is = null;
		try {
			// execute http post
			response = httpclient.execute(get);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				is = entity.getContent();
				return IOUtils.toString(is);
			}
		} catch (Exception e) {
			logger.error(url, e);
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (response != null)
					response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 发起GET请求，并以字符串形式返回结果
	 * 
	 * @param url
	 *            请求的目的url
	 * @param body 请求body
	 * @return 字符串形式的返回结果, 如果出错或没有结果则返回null.
	 */
	public static String post(String url, String body) {
		HttpPost post = new HttpPost(url);
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		InputStream is = null;
		try {
			// Request body
			post.setEntity(new StringEntity(body, Contants.charset));

			// execute http post
			response = httpclient.execute(post);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				is = entity.getContent();
				return IOUtils.toString(is);
			}
		} catch (Exception e) {
			logger.error(url, e);
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (response != null)
					response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
